package com.qf.im.service.impl;

import com.qf.im.entity.bo.HouseEsBo;
import com.qf.im.entity.vo.SearchParams;
import com.qf.im.service.IHourseSearchService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class HourseSearchServiceImpl implements IHourseSearchService {

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    /**
     * 创建索引库
     * @return
     */
    @Override
    public boolean createIndex() {
        IndexOperations indexOps = elasticsearchRestTemplate.indexOps(HouseEsBo.class);
        if (!indexOps.exists()) {
            log.debug("【es index create】创建ES的房屋索引信息.....");
            //创建索引库
            indexOps.create();
            //创建映射类型
            indexOps.putMapping();
            return true;
        }
        return false;
    }

    /**
     * 新增房屋
     * @param houseEsBo
     * @return
     */
    @Override
    public int save(HouseEsBo houseEsBo) {
        elasticsearchRestTemplate.save(houseEsBo);
        return 1;
    }

    /**
     * 搜索
     * @param searchParams
     * @return
     */
    @Override
    public List<HouseEsBo> search(SearchParams searchParams) {

        //构建查询条件
        BoolQueryBuilder mainQuery = QueryBuilders.boolQuery();
        //匹配关键字
        if (StringUtils.hasLength(searchParams.getKeyword())) {
            mainQuery.must().add(QueryBuilders.multiMatchQuery(searchParams.getKeyword(),
                    "subject", "subject.py", "info", "info.py", "address"));
        }

        //房屋类型查询
        if (searchParams.getType() != null) {
            mainQuery.must().add(QueryBuilders.matchQuery("type", searchParams.getType()));
        }

        //房屋价格查询
        searchParams.setMinPrice(searchParams.getMinPrice() != null ? searchParams.getMinPrice() : 0D);
        searchParams.setMaxPrice(searchParams.getMaxPrice() != null ? searchParams.getMaxPrice() : Double.MAX_VALUE);
        mainQuery.must().add(QueryBuilders.rangeQuery("price").gte(searchParams.getMinPrice())
                .lt(searchParams.getMaxPrice()));

        Query query = new NativeSearchQuery(mainQuery);


        //设置高亮
//        HighlightBuilder highlightBuilder = new HighlightBuilder();
//        highlightBuilder
//                .field("hotalName")
//                .preTags("<font color='red'>")
//                .postTags("</font>");
//        query.setHighlightQuery(new HighlightQuery(highlightBuilder.));

        //进行查询
        SearchHits<HouseEsBo> searchHits = elasticsearchRestTemplate.search(query, HouseEsBo.class);
        List<HouseEsBo> houseEsBos = searchHits.getSearchHits().stream()
                .map(searchHit -> searchHit.getContent()).collect(Collectors.toList());
        return houseEsBos;
    }
}
